package com.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utils.DBUtility;
import com.utils.GenerateTime;
import com.utils.GenerateUnameAndPwd;

public class AddNewAccountServlet extends HttpServlet {
	
	private Connection conn;
	private PreparedStatement pst;
	private ResultSet rs;
	/**
	 * Constructor of the object.
	 */
	public AddNewAccountServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {


			this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int i = 0;
		
		String userid = request.getParameter("userid");
		
		String addTime = GenerateTime.getCurrentTime();
		
		String genUsername = GenerateUnameAndPwd.genRandomUserCode( 8 );
		String genUserPwd = GenerateUnameAndPwd.genRandomPWD( 8 );
		
		System.out.println("AddNewAccountServlet==========>added time" +  addTime );
		System.out.println("AddNewAccountServlet==========>user name:" + genUsername);
		System.out.println("AddNewAccountServlet==========>password:" + genUserPwd );
		
		i = this.addNew( userid,genUsername,genUserPwd,addTime);
		
		System.out.println(i);
		out.print(i );
		out.flush();
		out.close();
	}

	
	
	
	/**
	 * @param adminId
	 * @param randomID
	 * @param randomPWd
	 * @param addTime
	 * @return
	 */
	private int addNew(String adminId, String randomID, String randomPWd, String addTime ){
		int i = 0;
		conn = DBUtility.getMySqlConnection();
		
		String sql = "INSERT INTO db.jiankong_account_table(account_use_type,account_type, userid,password,add_time,belongwho,used_flag,end_time)VALUES(?,?,?,?,?,?,?,?)";
		
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, "avilable");
			pst.setString(2, "test");
			pst.setString(3, randomID);
			pst.setString(4, randomPWd);
			pst.setString(5, addTime );
			pst.setString(6, adminId);
			pst.setString(7, "no" );
			pst.setString(8, GenerateTime.getEndTime() );
			
			i = pst.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			closeAllConnections();
		}
		return i;
	}
	/**
	 */
	private void closeAllConnections(){

		if( rs != null ){

			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if( pst != null){

			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}


	}
	public void init() throws ServletException {
		// Put your code here
	}

}
